package com.gopay.common.security.validcode.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.validcode.CodeType;
import com.gopay.common.domain.vs.VsValidCodeDtl;
import com.gopay.common.security.validcode.dao.ValidCodeDAO;

/**
 * 验证码DAO实现
 * 
 * @ValidCodeDAOImpl.java
 * @author yaobo
 * @2013-5-16 上午10:02:35 www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("validCodeDAO")
public class ValidCodeDAOImpl extends BaseDAOImpl<VsValidCodeDtl, Integer> implements ValidCodeDAO {

    @Override
    public void delete(CodeType type, String voID) {
        String hql = "delete from VsValidCodeDtl where type = " + type.value + " and voID = '" + voID + "'";

        this.deleteByHql(hql);
    }

    @Override
    public List<VsValidCodeDtl> getValidCodes(CodeType type, String voID) {
        String hql = "from VsValidCodeDtl where type = :type and voID = :voID and created = modified";

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("type", type.value);
        params.put("voID", voID);

        List<VsValidCodeDtl> list = this.findByHql(hql, params);

        return list;
    }
    
    @Override
    public List<VsValidCodeDtl> getValidCodeAll(CodeType type, String voID) {
        String hql = "from VsValidCodeDtl where type = :type and voID = :voID ";

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("type", type.value);
        params.put("voID", voID);

        List<VsValidCodeDtl> list = this.findByHql(hql, params);

        return list;
    }

    @Override
    public VsValidCodeDtl getValidCode(CodeType type, String voID) {
        String hql = "from VsValidCodeDtl where type = :type and voID = :voID";

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("type", type.value);
        params.put("voID", voID);

        List<VsValidCodeDtl> list = this.findByHql(hql, params);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }

        return null;
    }

   

}
